//
//  ATLocal.m
//  VRBOX
//
//  Created by CoderLT on 16/4/6.
//  Copyright © 2016年 VR. All rights reserved.
//

#import "ATLocal.h"
#import <KVOController/KVOController.h>


NSString * const ATUserLoginNotification = @"ATUserLoginNotification";
NSString * const ATUserLogoutNotification = @"ATUserLogoutNotification";
NSString * const ATReadyForSaleNotification = @"ATReadyForSaleNotification";
NSString * const ATEnterpriseUpdateNotification = @"ATEnterpriseUpdateNotification";
@implementation ATLocalSimpleModel
@end

@interface ATLocal ()
@property (nonatomic, strong) YTKKeyValueStore *resDB;
@property (nonatomic, strong) ATUserModel *user;
@property (nonatomic, strong) ATLocalSimpleModel *simpleModel;
@end

@implementation ATLocal
static ATLocal *_instance;
+ (instancetype)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init];
    });
    return _instance;
}
+ (YTKKeyValueStore *)resDB {
    return [[self sharedInstance] resDB];
}
- (YTKKeyValueStore *)resDB {
    if (_resDB == nil) {
        _resDB = [[YTKKeyValueStore alloc] initDBWithName:@".appDataResource"];
        [_resDB createTableWithName:[self localStroeTableName]];
    }
    return _resDB;
}
- (NSString *)localStroeTableName {
    return @"localStroeTableName";
}

#pragma mark - 用户数据 持久化
- (NSString *)simpleId {
    return @"simpleId";
}
+ (void)autoUpdateSimple {
    ATLocalSimpleModel *model = [self simple];
    [[ATLocal sharedInstance].KVOController unobserve:model];
    YYClassInfo *classInfo = [YYClassInfo classInfoWithClass:[ATLocalSimpleModel class]];
    [[ATLocal sharedInstance].KVOController observe:model keyPaths:classInfo.propertyInfos.allKeys options:NSKeyValueObservingOptionNew block:^(ATLocal *local, id  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
        [local.resDB putObject:[object modelToJSONObject] withId:local.simpleId intoTable:local.localStroeTableName];
    }];
}
+ (ATLocalSimpleModel *)simple {
    ATLocal *local = [ATLocal sharedInstance];
    if (!local.simpleModel) {
        local.simpleModel = [ATLocalSimpleModel modelWithJSON:[local.resDB getObjectById:local.simpleId fromTable:local.localStroeTableName]];
        if (!local.simpleModel) {
            local.simpleModel = [[ATLocalSimpleModel alloc] init];
        }
        [self autoUpdateSimple];
    }
    return local.simpleModel;
}
+ (void)setReadyForSale:(BOOL)readyForSale {
    if ([ATLocal simple].readyForSale) {
        return;
    }
    [ATLocal simple].readyForSale = readyForSale;
    [[NSNotificationCenter defaultCenter] postNotificationName:ATReadyForSaleNotification object:@([ATLocal simple].readyForSale)];
}
+ (void)setIsEnterprise:(BOOL)isEnterprise {
    if ([ATLocal simple].isEnterprise == isEnterprise) {
        return;
    }
    [ATLocal simple].isEnterprise = isEnterprise;
    [[NSNotificationCenter defaultCenter] postNotificationName:ATEnterpriseUpdateNotification object:@([ATLocal simple].isEnterprise)];
}
+ (BOOL)isEnterprise {
    return [ATLocal simple].isEnterprise;
}

#pragma mark - 用户数据 持久化
- (NSString *)userModelId {
    return @"userModelId";
}
+ (void)loginWithUser:(ATUserModel *)user {
    [self updateUser:user];
    [ATLocal setReadyForSale:![ATOnline isReviewing]];
    [[NSNotificationCenter defaultCenter] postNotificationName:ATUserLoginNotification object:[ATLocal user]];
}
+ (void)loginOut {
    ATUserModel *user = [self user];
    [self updateUser:nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:ATUserLogoutNotification object:user];
}
+ (void)updateUser:(ATUserModel *)user {
    ATLocal *local = [self sharedInstance];
    if (local.user && user) {
        [local.user modelSetWithJSON:user.modelToJSONObject];
    }
    else {
        local.user = user;
    }
    if (local.user) {
        [local.resDB putObject:local.user.modelToJSONObject withId:local.userModelId intoTable:local.localStroeTableName];
    }
    else {
        [local.resDB deleteObjectById:local.userModelId fromTable:local.localStroeTableName];
    }
    [self reloadUser];
}
+ (ATUserModel *)user {
    ATLocal *local = [self sharedInstance];
    if (!local.user) {
        [self reloadUser];
    }
    return local.user;
}
+ (void)reloadUser {
    ATLocal *local = [self sharedInstance];
    local.user = [ATUserModel modelWithJSON:[local.resDB getObjectById:local.userModelId fromTable:local.localStroeTableName]];
}
+ (BOOL)isLogin {
    return self.user.isLogin;
}
@end
